package com.gvitz.transaction.database;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import com.gvitz.transaction.models.Client;
import com.gvitz.transaction.utils.Config;

public class webserviceVendedores {
		
	 public ProgressDialog pDialog; 
	 public Context ctx;
	 public Activity act;
  	 private static final String NAMESPACE = "http://gvitz_webservice/";
  	 private static final String URL = Config.IP + "WSClients?wsdl"; 
	 
	 
	public webserviceVendedores(Context ctx, Activity act) {
		super();
		this.ctx = ctx;
		this.act = act;
		Config.trustEveryone();
	}

  public void getVendedor(String id_vendedor){
	    
	     new getVendedorA().execute(id_vendedor);
  }
  
  public void getVendedores(String fecha){
	    
	     new getVendedoresA().execute(fecha);
}
  
  public class getVendedoresA extends AsyncTask< String, String, String > {
	  	
	   	 private static final String METHOD_NAME ="sentV";
	   	 private static final String SOAP_ACTION =  "http://gvitz_webservice/sentV";
	   	 String fecha, result;
	   		 
	        protected void onPreExecute() {	   		    
	        //nada de momento
	        }
	 
			@SuppressWarnings("deprecation")
			protected String doInBackground(String... params) {
				
	              fecha =params[0];

				  SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

				  PropertyInfo propInfo=new PropertyInfo();
				  propInfo.name="fecha";
				  propInfo.type=PropertyInfo.STRING_CLASS;
				  
				  request.addProperty(propInfo,fecha); 


				  //realizar pedido
				  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
				  envelope.setOutputSoapObject(request);			  
				  HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
						
					  try {
					   
						  //realizar peticion
						   androidHttpTransport.call(SOAP_ACTION, envelope);				  
						   SoapObject  resultsRequestSOAP = (SoapObject) envelope.bodyIn;
						   int count = resultsRequestSOAP.getPropertyCount();
						   
						   gvitzDB dbHelper= new gvitzDB(ctx);
						   
						   for (int i=0; i<count; i++){
							   SoapObject result =(SoapObject) resultsRequestSOAP.getProperty(i);
							   Client v = new Client(Integer.parseInt(result.getProperty(3).toString()), 
                         		   								  result.getProperty(6).toString(),
                         		                                  result.getProperty(4).toString(), 
                         		                                  result.getProperty(5).toString(), 
                         		                                  result.getProperty(1).toString());
							   dbHelper.UpdateClient(v);

							   }
						  
						   result = "ok";
					   
					  } catch (Exception e) {
						  e.printStackTrace();
						  result="SOAP";						 
					  }
				return result;	
			}
	       
	        protected void onPostExecute(String result) {                   							    
	        	// nada de momento	                    
	          }		
	        }
  
  public class getVendedorA extends AsyncTask< String, String, String > {
	  	
	   	 private static final String METHOD_NAME ="sentVS";
	   	 private static final String SOAP_ACTION =  "http://gvitz_webservice/sentVS";
	   	 String id_vendedor, result;
	   		 
	        protected void onPreExecute() {	   		    
	        	pDialog = new ProgressDialog(act);
	            pDialog.setMessage("Buscando local");
	            pDialog.setIndeterminate(false);
	            pDialog.setCancelable(false);
	            pDialog.show();
	        }
	 
			@SuppressWarnings("deprecation")
			protected String doInBackground(String... params) {
				
				id_vendedor =params[0];

				  SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

				  PropertyInfo propInfo=new PropertyInfo();
				  propInfo.name="id_vendedor";
				  propInfo.type=PropertyInfo.STRING_CLASS;
				  
				  request.addProperty(propInfo,id_vendedor); 


				  //realizar pedido
				  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
				  envelope.setOutputSoapObject(request);			  
				  HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
						
					  try {
					   
						  //realizar peticion
						   androidHttpTransport.call(SOAP_ACTION, envelope);				  
						   SoapObject  resultsRequestSOAP = (SoapObject) envelope.bodyIn;
						   int count = resultsRequestSOAP.getPropertyCount();
						   
						   gvitzDB dbHelper= new gvitzDB(ctx);
						   
						   for (int i=0; i<count; i++){
							   SoapObject result =(SoapObject) resultsRequestSOAP.getProperty(i);

                            Client v = new Client(Integer.parseInt(result.getProperty(3).toString()), 
                         		   								  result.getProperty(6).toString(),
                         		                                  result.getProperty(4).toString(), 
                         		                                  result.getProperty(5).toString(), 
                         		                                  result.getProperty(1).toString());
							   dbHelper.AddClient(v);

							   }
						  
						   if (count == 0){
							   result = "errLocal";   
						   }
						   else{
						   result = "ok";
						   }
					   
					  } catch (Exception e) {
						  
						  e.printStackTrace();
						  result="Error de Conexion";	
						  
					  }
				return result;	
			}
	       
	        protected void onPostExecute(String result) {                   							    
	        	pDialog.dismiss();
	        	
	        	if (result.equals("ok")){
	        		
	        		result = "Local encontrado";
	        		
	        	}
	        	else{
	        		if(result.equals("errLocal")){
	        		
	        			result = "Local NO encontrado";
	        			
	        		}
	        		else{
	        			
	        			result = "Error al buscar Local";
	        		}
	        	}
	        	
	        	Toast.makeText(ctx,result, Toast.LENGTH_LONG).show();
	        	
	          }		
	        }


}
